package org.one.stone.soup.grfxML;

import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.xml.XmlElement;
  /*
 * Copyright 2000-2001 Wet-Wired.com Ltd.,
 * Portsmouth England
 * All rights reserved.
 */

/**
	*
	* @author Nik Cross
*/
public class DataArc extends DataGeometry implements ITag {
	public static String DEFAULT_ID = "_Arc";
		DataSize radius = new DataSize(10);
		DataSize radiusView = new DataSize(10);
		DataSize startAngle = new DataSize(TagModifier.SIZE_TYPE_ANGLE, 0);
		DataSize startAngleView = new DataSize(TagModifier.SIZE_TYPE_ANGLE, 0);
		DataSize sweepAngle = new DataSize(TagModifier.SIZE_TYPE_ANGLE, 0);
		DataSize sweepAngleView = new DataSize(TagModifier.SIZE_TYPE_ANGLE, 0);

		GeometryArc _arc = null;
		DataTranslation _translation = null;
public DataArc() {
	try{
		setId(DEFAULT_ID,this);
	}
	catch(LockException le){} //Dummy catch
}
public void buildFromgrfxML(XmlElement xml)
{
	String name = xml.getAttributeValueByName(Token.ID);
	if(name!=null)
	{
		try{
			setId(name,this);
		}
		catch(LockException le){} //Dummy catch
	}

	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.RADIUS))
		{
			radius = new DataSize(TagModifier.SIZE_TYPE_SIZE,10);
			radius.buildFromgrfxML(element,TagModifier.SIZE_TYPE_SIZE);
		}
		else if(eleName.equals(Token.START_ANGLE))
		{
			startAngle = new DataSize(TagModifier.SIZE_TYPE_ANGLE,0);
			startAngle.buildFromgrfxML(element,TagModifier.SIZE_TYPE_ANGLE);
		}
		else if(eleName.equals(Token.SWEEP_ANGLE))
		{
			sweepAngle = new DataSize(TagModifier.SIZE_TYPE_ANGLE,0);
	  		sweepAngle.buildFromgrfxML(element,TagModifier.SIZE_TYPE_ANGLE);
		}
		else if(super.buildFromElement(element)==false && !NameHelper.isSystemName(element.getName()))
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.ARC,""+element.getStartLineNo()} );
		}
	}
}
public void copyFromgrfxML(XmlElement xml)
{

	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.RADIUS))
		{
			radius.copyFromgrfxML(element,TagModifier.SIZE_TYPE_SIZE);
		}
		else if(eleName.equals(Token.START_ANGLE))
		{
			startAngle.copyFromgrfxML(element,TagModifier.SIZE_TYPE_ANGLE);
		}
		else if(eleName.equals(Token.SWEEP_ANGLE))
		{
	  		sweepAngle.copyFromgrfxML(element,TagModifier.SIZE_TYPE_ANGLE);
		}
		else if(super.copyFromElement(element)==false)
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.ARC,""+element.getStartLineNo()} );
		}
	}
}
public DataGeometry getClone() {

	DataArc newArc = new DataArc();

	super.getClonedElements(newArc);

	newArc.radius = radius.getClone();
	newArc.startAngle = startAngle.getClone();
	newArc.sweepAngle = sweepAngle.getClone();

	return newArc;
}
public DataGeometry getClone(String path) {

	DataArc newArc = new DataArc();

	GrfxMLStoreProcessor.setCloneName( path,this,newArc );

	super.getClonedElements(path,newArc);

	newArc.radius = radius.getClone(path);
	newArc.startAngle = startAngle.getClone(path);
	newArc.sweepAngle = sweepAngle.getClone(path);

	return newArc;
}
public Geometry getGeometry(GrfxMLEngine engine)
{
	if(_arc==null && _translation!=null && color!=null)
	{
		_arc = new GeometryArc(
				engine,
				_translation.window,
				bounds,
				_translation.visibleView,
				transparency,
				color,
				filled,
				_translation.positionView,
				offset,
				offsetView,
				startAngleView,
				sweepAngle,
				radiusView
			);
		if( !NameHelper.isSystemName(getId()) )
		{
			_arc.setId(getId());
		}
	}
	return(_arc);
}
public DataSize getRadius() {
	return radius;
}
public DataSize getStartAngle() {
	return startAngle;
}
public DataSize getSweepAngle() {
	return sweepAngle;
}
public void register(DataLinker store) {

	super.register(store);

	try{
		store.add(radius,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(startAngle,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(sweepAngle,this);
	}
	catch(LockException le)
	{}//Dummy catch
}
public void replace(Field oldObj, Field newObj) {
	replace(oldObj, newObj, false);
}
public void replace(Field oldObj, Field newObj,boolean recursive) {

	_arc=null;

	super.replace(oldObj,newObj);

	if(radius==oldObj)
		radius=(DataSize)newObj;

	if(startAngle==oldObj)
		startAngle=(DataSize)newObj;

	if(sweepAngle==oldObj)
		sweepAngle=(DataSize)newObj;
}
public void resetChanged() {
	super.resetChanged();
	radius.resetChanged();
	radiusView.resetChanged();
	startAngle.resetChanged();
	startAngleView.resetChanged();
	sweepAngle.resetChanged();
}
protected void setRadius(DataSize newRadius) {
	radius = newRadius;
}
protected void setStartAngle(DataSize newStartAngle) {
	startAngle = newStartAngle;
}
protected void setSweepAngle(DataSize newSweepAngle) {
	sweepAngle = newSweepAngle;
}
public String togrfxML(String name) {
	String xml = "<" + name;

	if(!NameHelper.isSystemName(getId()))
	{
		xml += " id=\""+getId()+"\"";
	}

	xml += ">";
	xml += super.togrfxML(name);
	xml += radius.togrfxML(new DataSize(10),Token.RADIUS);
	xml += startAngle.togrfxML(Token.START_ANGLE);
	xml += sweepAngle.togrfxML(Token.SWEEP_ANGLE);
	xml += "</" + name + ">\n";
	return xml;
}
public String toString() {
	if (GrfxMLEngine.DEBUG)
		return (togrfxML(DEFAULT_ID.substring(1)));
	String xml = "<" + DEFAULT_ID.substring(1) + "/>";
	return xml;
}
public void translate(DataTranslation t)
{
 	if(_translation==null)
		_translation=t;

	try{
		GrfxMLProcessor.translate(radius,TagModifier.SIZE_TYPE_SIZE,t,radiusView);
	}
	catch(LockException le)
	{
		le.printStackTrace();
	}

	try{
		GrfxMLProcessor.translate(startAngle,TagModifier.SIZE_TYPE_ANGLE,t,startAngleView);
	}
	catch(LockException le)
	{
		le.printStackTrace();
	}

	super.translate(t);
}
}
